package com.dwd.basic.service.impl;

import com.dwd.basic.mapper.BaseMapper;
import com.dwd.basic.query.BaseQuery;
import com.dwd.basic.service.IBaseService;
import com.dwd.basic.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/12 11:23
 * @Version 1.0
 */
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class BaseServiceImpl<T> implements IBaseService<T> {
    /**
     * 注意加泛型，因为存在多个mapper，不唯一
     */
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public T queryById(Long id) {
        return baseMapper.queryById(id);
    }

    @Override
    public List<T> queryAll() {
        return baseMapper.queryAll();
    }

    @Override
    public Long queryCount(BaseQuery query) {
        return baseMapper.queryCount(query);
    }

    @Override
    public PageBean<T> queryPage(BaseQuery query) {
        //总条数
        Long count = baseMapper.queryCount(query);
        if (count == 0) {
            return new PageBean<T>();
        }
        List<T> departments = baseMapper.queryPage(query);
        return new PageBean<>(count, departments);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchRemove(List<T> list) {
        baseMapper.batchRemove(list);
    }
}
